from django.shortcuts import render,redirect
from django.http import HttpResponse
from django.views import View
from website import models
# Create your views here.
KEYWORD='安徽九合堂玄鸟健康科技有限公司'
DESCRIPTION='安徽九合堂玄鸟健康科技有限公司'
class HomeView(View):
    """
    HomeView for website views.
    """
    def get(self, request):
        obj=models.WebInfoModel.objects.filter(state='1').first()
        products = models.WebProducts.objects.filter(state='1', enable='1')
        newslist=models.WebNews.objects.filter(state='1', enable='1')
        content={
            'title':'九合堂官网',
            'webinfo':obj,
            'products':products,
            'newslist':newslist,
        }

        return render(request,'website/index.html',content)

class AboutView(View):
    """
    HomeView for website views.
    """
    def get(self, request):
        obj=models.WebPage.objects.filter(state='1').first()
        webinfo = models.WebInfoModel.objects.filter(state='1').first()

        content={
            'title':'九合堂官网',
            'keyword':KEYWORD,
            'description':DESCRIPTION,
            'webpage':obj,
            'webinfo': webinfo,

        }

        return render(request,'website/about.html',content)

class ProductView(View):
    """
    HomeView for website views.
    """
    def get(self, request):
        obj=models.WebPage.objects.filter(state='1').first()
        webinfo = models.WebInfoModel.objects.filter(state='1').first()
        products=models.WebProducts.objects.filter(state='1').all()
        content={
            'title':'九合堂官网',
            'keyword':KEYWORD,
            'description':DESCRIPTION,
            # 'webpage':obj,
            'webinfo': webinfo,
            'products':products,
        }

        return render(request,'website/prodlist.html',content)

class ProductOneView(View):
    """
    HomeView for website views.
    """
    def get(self, request,id):
        obj=models.WebPage.objects.filter(state='1').first()
        webinfo = models.WebInfoModel.objects.filter(state='1').first()
        product=models.WebProducts.objects.filter(state='1',pk=id).first()
        content={
            'title':'九合堂官网',
            'keyword':KEYWORD,
            'description':DESCRIPTION,
            # 'webpage':obj,
            # 'webinfo': webinfo,
            'product':product,
        }

        return render(request,'website/product.html',content)

class NewsView(View):
    """
    HomeView for website views.
    """
    def get(self, request,page=1):
        page_size = 2  # 每页显示2条数据
        current_page = int(page)  # 将传入的page参数转为整数
        obj=models.WebPage.objects.filter(state='1').first()
        webinfo = models.WebInfoModel.objects.filter(state='1').first()

        # 分页查询修正：Django中切片用冒号:而不是逗号,
        # 同时需要处理页码小于1的情况
        start = (current_page - 1) * page_size
        end = current_page * page_size
        newslist = models.WebNews.objects.filter(state='1')[start:end]  # 修正为冒号
        total_records=models.WebNews.objects.filter(state='1').count()
        # 处理总页数计算（向上取整，避免小数和余数问题）
        if total_records % page_size == 0:
            total_pages = total_records // page_size
        else:
            total_pages = (total_records // page_size) + 1

        # 处理总页数为0的特殊情况（没有数据时）
        total_pages = max(total_pages, 1)
        content={
            'title':'九合堂官网',
            'keyword':KEYWORD,
            'description':DESCRIPTION,
            # 'webpage':obj,
            'webinfo': webinfo,
            'newslist':newslist,
            'current_page':current_page,
            'page_range' : range(current_page - 1, current_page + 2),
            'total_pages':total_pages,
            'total_records':total_records
        }

        return render(request,'website/news.html',content)

class NewView(View):
    """
    HomeView for website views.
    """
    def get(self, request,id):
        obj=models.WebPage.objects.filter(state='1').first()
        webinfo = models.WebInfoModel.objects.filter(state='1').first()
        article=models.WebNews.objects.filter(state='1',pk=id).first()

        content={
            'title':'九合堂官网',
            'keyword':KEYWORD,
            'description':DESCRIPTION,
            # 'webpage':obj,
            'webinfo': webinfo,
            'article':article,

        }

        return render(request,'website/shownews.html',content)


class FeedbackView(View):
    """
    HomeView for website views.
    """
    def get(self, request):
        # obj=models.WebPage.objects.filter(state='1').first()
        webinfo = models.WebInfoModel.objects.filter(state='1').first()
        # article=models.WebNews.objects.filter(state='1',pk=id).first()

        content={
            'title':'九合堂官网',
            'keyword':KEYWORD,
            'description':DESCRIPTION,
            # 'webpage':obj,
            'webinfo': webinfo,
            # 'article':article,

        }

        return render(request,'website/feedback.html',content)

    def post(self,request):

        models.WebMessageModel.objects.create(linkman=request.POST.get('name',''),
                                              phone=request.POST.get('tel',''),
                                              title=request.POST.get('title', ''),
                                              email=request.POST.get('email', ''),
                                              address=request.POST.get('add', ''),
                                              contents=request.POST.get('contents', ''),
                                              )
        # return redirect('/website/')
        return HttpResponse("<script>alert('提交成功,我们会尽快联系您');location.href='/';</script>")


class ContactView(View):
    """
    HomeView for website views.
    """
    def get(self, request):
        # obj=models.WebPage.objects.filter(state='1').first()
        webinfo = models.WebInfoModel.objects.filter(state='1').first()
        # article=models.WebNews.objects.filter(state='1',pk=id).first()

        content={
            'title':'九合堂官网',
            'keyword':KEYWORD,
            'description':DESCRIPTION,
            # 'webpage':obj,
            'webinfo': webinfo,
            # 'article':article,

        }

        return render(request,'website/contact.html',content)

